<template>
  <div class="myFavorite">
    <!-- 头部 -->
    <van-nav-bar
      style="background-color:#f2f2f2"
      title="我的收藏"
      left-text=""
      right-text=""
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="favoriteBox">
      <div class="goodsIteminfo" v-for="item in favoriteArr" :key="item.cp_id">
        <div class="goodsIteminfoImg">
          <img :src="item.cp_tupian" alt="图片" />
        </div>
        <div class="goodsIteminfoMsg">
          <h5>{{ item.cp_mingcheng }}</h5>
          <p class="tips">
            库存：{{ item.kucun }} | 限购：{{ item.xiangou_shuliang }} 件
          </p>
          <p class="setPrice">
            <span style="color: #ee0a24">￥{{ item.jiage }}</span>
            <van-icon name="delete" size="20" @click="delFavorite(item)" />
          </p>
        </div>
      </div>
      <!-- 数据为空 -->
      <van-empty v-if="favoriteArr.length === 0" description="暂时没有收藏" />
    </div>
  </div>
</template>

<script>
import SERVE from "@/axios/index";
export default {
  name: "myFavorite",
  data() {
    return {
      favoriteArr: [],
    };
  },
  mounted() {
    this.$toast.loading({
      mask: true,
      duration: 0,
      message: "加载中..."
    });
    this.getMyFavorite();
  },
  methods: {
    onClickLeft() {
      this.$router.push("/vip");
    },
    getMyFavorite() {
      SERVE.getMyFavorite({
        params: {
          uid: window.localStorage.u_id,
        },
      })
        .then((res) => {
          this.$toast.clear();
          this.favoriteArr = res.data;
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err", err);
        });
    },
    // 删除
    delFavorite(item) {
      this.$dialog
        .confirm({
          title: "提示",
          message: "是否删除这条收藏？",
        })
        .then(() => {
          SERVE.delMyFavorite({
            params: {
              uid: window.localStorage.u_id,
              scid: item.sc_id,
            },
          })
            .then((res) => {
              if (res.data.zt === "yes") {
                this.getMyFavorite();
              }
              this.$notify({ type: res.data.zt === "yes" ? "success" : "warning", message: res.data.xinxi });
            })
            .catch((err) => {
              console.log("err", err);
            });
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.myFavorite {
  .favoriteBox {
    height: calc(100vh - 46px);
    overflow-y: scroll;
    .goodsIteminfo {
      border-bottom: 2px solid #f2f2f2;
      min-height: 100px;
      padding: 0 15px;
      box-sizing: border-box;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      word-break: break-all;
      margin-bottom: 1px;
      .goodsIteminfoImg {
        width: 80px;
        height: 80px;
        margin-right: 10px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .goodsIteminfoMsg {
        width: calc(100% - 90px);
        height: 100%;
        padding: 10px 0;
        box-sizing: border-box;
        h5 {
          margin: 0;
        }
        .tips {
          font-size: 12px;
          margin: 6px 0;
        }
        .setPrice {
          display: flex;
          justify-content: space-between;
          align-items: center;
        }
      }
    }
  }
}
</style>
